import axios from 'axios'
class TApi {
  constructor() {
    this.config = {
      localKey: 'appApiLocalKey',
    }
    this.pageState = {}
  }
  callApi(apiUrl, compCode, action, params, method = 'post') {
    var me = this
    var res = {}
    return new Promise((su, fa) => {
      try {
        var url = [apiUrl, compCode, action].join('/')
        console.log(url)
        if (method.toLowerCase() == 'post') {
          var ps = params && params != null ? params : {}
          axios
            .post(url, ps, {
              headers: {
                'Content-Type': 'application/json',
              },
            })
            .then((res) => {
              su(res.data)
            })
            .catch((error) => {
              console.log(error)
              su(res)
            })
        } else if (method.toLowerCase() == 'get') {
          var ps = params && params != null ? params : {}
          if (Object.keys(ps).length > 0) {
            if (url.indexOf('?') > 0) {
              url = url + '&'
            } else {
              url = url + '?'
            }
            var kvs = Object.keys(ps).map((k) => {
              return k + '=' + ps[k]
            })
            url = url + kvs.join('&')
          }
          axios.get(url).then((_res) => {
            su(_res.data)
          })
        } else {
          su()
        }
      } catch (error) {
        su(res)
      }
    })
  }
  getSessionObj(key, defaultObj) {
    var res = defaultObj
    try {
      var data = window.sessionStorage.getItem(key)
      if (data && data != null && data != '') {
        res = JSON.parse(data)
      }
    } catch (error) {
      console.log(error)
    }
    return res
  }
  setSessionObj(key, obj) {
    try {
      window.sessionStorage.setItem(key, JSON.stringify(obj))
    } catch (error) {
      console.log(error)
    }
  }
}

class TFeTsApi {
  constructor() {
    this.apiUrl = '/apiFets'
    this.apiPath = 'fetsComp'
    this.api = api
    this.actionCode = {
      duelReset: 'duelReset',
      duelStart: 'duelStart',
      duelStop: 'duelStop',
      setReset: 'setReset',
      setStart: 'setStart',
      setStop: 'setStop',
    }
  }
  // 获取字典
  getDicts() {
    var me = this
    var res = {}
    return new Promise((su, fa) => {
      try {
        api.callApi(me.apiUrl, me.apiPath, 'getDicts', {}).then((_res) => {
          if (_res && _res.status == 1) {
            res = _res.result
          }
          su(res)
        })
      } catch (error) {
        console.log(error)
        su(res)
      }
    })
  }
  getDuelList(scheduleStatus, phaseCode) {
    var me = this
    var res = {
      status: 0,
    }
    return new Promise((su, fa) => {
      try {
        api
          .callApi(me.apiUrl, me.apiPath, 'getDuelList', { scheduleStatus, phaseCode })
          .then((_res) => {
            if (_res && _res.status == 1) {
              res = _res.result
            }
            su(res)
          })
      } catch (error) {
        console.log(error)
        su(res)
      }
    })
  }
  getDuelInfo(duelId) {
    var me = this
    var res = {
      status: 0,
    }
    return new Promise((su, fa) => {
      try {
        api.callApi(me.apiUrl, me.apiPath, 'getDuelInfo', { duelId }).then((_res) => {
          if (_res && _res.status == 1) {
            res = _res.result
          }
          su(res)
        })
      } catch (error) {
        console.log(error)
        su(res)
      }
    })
  }
  // 获取第几局的数据
  getSetInfo(setKey) {
    var me = this
    var res = {
      status: 0,
    }
    return new Promise((su, fa) => {
      try {
        api.callApi(me.apiUrl, me.apiPath, 'getSetInfo', { setKey }).then((_res) => {
          if (_res && _res.status == 1) {
            res = _res.result
          }
          su(res)
        })
      } catch (error) {
        console.log(error)
        su(res)
      }
    })
  }
  // 获取裁判器数据
  getReferee() {
    var me = this
    var res = {
      status: 0,
    }
    return new Promise((su, fa) => {
      try {
        api.callApi(me.apiUrl, me.apiPath, 'getReferee', {}).then((_res) => {
          if (_res && _res.status == 1) {
            res = _res.result
          }
          su(res)
        })
      } catch (error) {
        console.log(error)
        su(res)
      }
    })
  }
  // 开始、停止、复位
  action(actionCode, duelId = '', setKey = '') {
    var me = this
    var res = {
      status: 0,
    }
    return new Promise((su, fa) => {
      try {
        console.log('action', actionCode, duelId, setKey)
        var ps = {}
        if (actionCode == 'duelReset' || actionCode == 'duelStart' || actionCode == 'duelStop') {
          ps.duelId = duelId
        } else if (
          actionCode == 'setReset' ||
          actionCode == 'setStart' ||
          actionCode == 'setStop'
        ) {
          ps.setKey = setKey
        }
        api.callApi(me.apiUrl, me.apiPath, actionCode, ps).then((_res) => {
          if (_res && _res.status == 1) {
            res = _res.result
          }
          su(res)
        })
      } catch (error) {
        console.log(error)
        su(res)
      }
    })
  }
  // 设置当前是第几局
  setDuelSetOrder(duelId, setOrder) {
    var me = this
    var res = {
      status: 0,
    }
    return new Promise((su, fa) => {
      try {
        api.callApi(me.apiUrl, me.apiPath, 'setDuelSetOrder', { duelId, setOrder }).then((_res) => {
          if (_res && _res.status == 1) {
            res = _res.result
          }
          su(res)
        })
      } catch (error) {
        console.log(error)
        su(res)
      }
    })
  }
}

const api = new TApi()
const fets = new TFeTsApi()

export default fets
